package cn.iocoder.yudao.module.acm.dal.mysql.stock;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.acm.controller.admin.stock.vo.in.AcmStockInPageReqVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.stock.AcmStockInDO;
import cn.iocoder.yudao.module.acm.dal.dataobject.stock.AcmStockInItemDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * ERP 其它入库单 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface AcmStockInMapper extends BaseMapperX<AcmStockInDO> {

    default PageResult<AcmStockInDO> selectPage(AcmStockInPageReqVO reqVO) {
        MPJLambdaWrapperX<AcmStockInDO> query = new MPJLambdaWrapperX<AcmStockInDO>()
                .likeIfPresent(AcmStockInDO::getNo, reqVO.getNo())
                .eqIfPresent(AcmStockInDO::getSupplierId, reqVO.getSupplierId())
                .betweenIfPresent(AcmStockInDO::getInTime, reqVO.getInTime())
                .eqIfPresent(AcmStockInDO::getStatus, reqVO.getStatus())
                .likeIfPresent(AcmStockInDO::getRemark, reqVO.getRemark())
                .eqIfPresent(AcmStockInDO::getCreator, reqVO.getCreator())
                .orderByDesc(AcmStockInDO::getId);
        if (reqVO.getWarehouseId() != null || reqVO.getProductId() != null) {
            query.leftJoin(AcmStockInItemDO.class, AcmStockInItemDO::getInId, AcmStockInDO::getId)
                    .eq(reqVO.getWarehouseId() != null, AcmStockInItemDO::getWarehouseId, reqVO.getWarehouseId())
                    .eq(reqVO.getProductId() != null, AcmStockInItemDO::getProductId, reqVO.getProductId())
                    .groupBy(AcmStockInDO::getId); // 避免 1 对多查询，产生相同的 1
        }
        return selectJoinPage(reqVO, AcmStockInDO.class, query);
    }

//    default PageResult<AcmStockInDO> selectAppPage(AppAcmStockInPageReqVO reqVO) {
//        MPJLambdaWrapperX<AcmStockInDO> query = new MPJLambdaWrapperX<AcmStockInDO>()
//                .likeIfPresent(AcmStockInDO::getNo, reqVO.getNo())
//                .eqIfPresent(AcmStockInDO::getSupplierId, reqVO.getSupplierId())
//                .betweenIfPresent(AcmStockInDO::getInTime, reqVO.getInTime())
//                .eqIfPresent(AcmStockInDO::getStatus, reqVO.getStatus())
//                .likeIfPresent(AcmStockInDO::getRemark, reqVO.getRemark())
//                .eqIfPresent(AcmStockInDO::getCreator, reqVO.getCreator())
//                .orderByDesc(AcmStockInDO::getId);
//        if (reqVO.getWarehouseId() != null || reqVO.getProductId() != null) {
//            query.leftJoin(AcmStockInItemDO.class, AcmStockInItemDO::getInId, AcmStockInDO::getId)
//                    .eq(reqVO.getWarehouseId() != null, AcmStockInItemDO::getWarehouseId, reqVO.getWarehouseId())
//                    .eq(reqVO.getProductId() != null, AcmStockInItemDO::getProductId, reqVO.getProductId())
//                    .groupBy(AcmStockInDO::getId); // 避免 1 对多查询，产生相同的 1
//        }
//        return selectJoinPage(reqVO, AcmStockInDO.class, query);
//    }


    default int updateByIdAndStatus(Long id, Integer status, AcmStockInDO updateObj) {
        return update(updateObj, new LambdaUpdateWrapper<AcmStockInDO>()
                .eq(AcmStockInDO::getId, id).eq(AcmStockInDO::getStatus, status));
    }

    default AcmStockInDO selectByNo(String no) {
        return selectOne(AcmStockInDO::getNo, no);
    }

}
